package com.mkh.st.common.job.client;

/**
 * 任务调度周期构建
 * @Description	
 * @author pc
 */
public class TaskScheduleBuilder {
	
	/**
	 * 秒
	 */
	private String second = "0";
	
	/**
	 * 分
	 */
	private String minute = "0";
	
	/**
	 * 时
	 */
	private String hour = "0";
	
	/**
	 * 月中的某天
	 */
	private String dayofMonth = "*";
	
	/**
	 * 某月
	 */
	private String month = "*";
	
	/**
	 * 周中的某天
	 */
	private String dayofWeek = "?";
	
	/**
	 * 年
	 */
	private String year = "";
	
	/**
	 * 获取实例
	 * @return
	 */
	public static TaskScheduleBuilder getInstance() {
		return new TaskScheduleBuilder();
	}
	
	/**
	 * 每隔多少小时执行
	 * @param hour
	 * @return
	 */
	public static TaskScheduleBuilder getEveryHourSchedule(String hour) {
		TaskScheduleBuilder scheduleBuilder = new TaskScheduleBuilder();
		scheduleBuilder.setHour("*/" + hour);
		return scheduleBuilder;
	}
	
	/**
	 * 每隔多少分钟执行
	 * @param minute
	 * @return
	 */
	public static TaskScheduleBuilder getEveryMinuteSchedule(String minute) {
		TaskScheduleBuilder scheduleBuilder = new TaskScheduleBuilder();
		scheduleBuilder.setMinute("*/" + minute);
		scheduleBuilder.setHour("*");
		return scheduleBuilder;
	}
	
	/**
	 * 每隔多少秒执行
	 * @param second
	 * @return
	 */
	public static TaskScheduleBuilder getEverySecondSchedule(String second) {
		TaskScheduleBuilder scheduleBuilder = new TaskScheduleBuilder();
		scheduleBuilder.setSecond("*/" + second);
		scheduleBuilder.setMinute("*");
		scheduleBuilder.setHour("*");
		return scheduleBuilder;
	}
	
	public TaskScheduleBuilder setSecond(String second) {
		this.second = second;
		return this;
	}

	public TaskScheduleBuilder setMinute(String minute) {
		this.minute = minute;
		return this;
	}

	public TaskScheduleBuilder setHour(String hour) {
		this.hour = hour;
		return this;
	}

	public TaskScheduleBuilder setDayofMonth(String dayofMonth) {
		this.dayofMonth = dayofMonth;
		this.dayofWeek = "?";
		return this;
	}

	public TaskScheduleBuilder setMonth(String month) {
		this.month = month;
		return this;
	}

	public TaskScheduleBuilder setDayofWeek(String dayofWeek) {
		this.dayofWeek = dayofWeek;
		this.dayofMonth = "?";
		return this;
	}

	public TaskScheduleBuilder setYear(String year) {
		this.year = year;
		return this;
	}

	/**
	 * 获取cron表达式
	 * @return
	 */
	public String getSchedule() {
		StringBuffer temp = new StringBuffer();
		temp.append(this.second).append(" ").append(this.minute).append(" ").append(this.hour)
		    .append(" ").append(this.dayofMonth).append(" ").append(this.month).append(" ")
		    .append(this.dayofWeek).append(" ").append(this.year);
		String cron = temp.toString().trim();
		return cron;
	}

}
